package com.tqw.auth.controller;

import com.tqw.auth.entity.JWTUtil;
import com.tqw.auth.entity.Result;
import com.tqw.auth.entity.StatusCode;
import com.tqw.auth.pojo.Client;
import com.tqw.auth.pojo.ClientLogin;
import com.tqw.auth.pojo.ClientSignIn;
import com.tqw.auth.service.LoginClientService;
import com.tqw.auth.service.RefreshService;
import com.tqw.auth.service.SignInClientService;
import com.tqw.constant.SecurityConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * @author Van
 */
@RestController
@RequestMapping("/client")
public class Oauth2ClientController {

    @Autowired
    private LoginClientService loginClientService;

    @Autowired
    private SignInClientService signInClientService;

    @Autowired
    private RefreshService refreshService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /*
     *  用户 登录 认证
     *  返回token令牌
     */
    @PostMapping("/login")
    public Result login(ClientLogin clientLogin){
        if (!"".equals(clientLogin.getPassword()) || !"".equals(clientLogin.getUsername())){
           Client client = loginClientService.loginClient(clientLogin);
           if (client!=null){
               redisTemplate.opsForValue().set(JWTUtil.sign(JWTUtil.getMap(client)),String.valueOf(client.getId()));
              //这里设置的超时时间是3小时
               redisTemplate.expire(JWTUtil.sign(JWTUtil.getMap(client)), 3, TimeUnit.HOURS);//这里设置的超时时间是两分钟
               return new Result(true,StatusCode.OK,"登录成功",JWTUtil.sign(JWTUtil.getMap(client)));
           }
            return new Result(false,StatusCode.ERROR,"用户名或密码错误");
        }
        return new Result(false,StatusCode.ERROR,"用户名或密码错误");
    }

    /*
     *  用户 注册
     */
    @PostMapping("/sign/in")
    public Result signin(@RequestBody ClientSignIn clientSignIn){
        if (clientSignIn.getEmail()!=null || clientSignIn.getPassword()!=null || clientSignIn.getPhone()!=null || clientSignIn.getUsername()!=null){
            if (signInClientService.addClient(clientSignIn))
                return new Result(true,StatusCode.OK,"注册成功");
        }
        return new Result(false, StatusCode.ERROR,"请按要求填信息");
    }

    /*
     *  判断token是否过期
     */
    @PostMapping("/standing/by")
    public Boolean standingBy(@RequestHeader(SecurityConstants.AUTH_TOKEN) String token){
        if (token==null)
            return false;
        System.out.println(!JWTUtil.isExpired(token));
        return !JWTUtil.isExpired(token);
    }

    /*
     *  解析token令牌中的载荷数据
     */
    @PostMapping("/standing")
    public Client standing(@RequestHeader(SecurityConstants.AUTH_TOKEN) String token){
        return JWTUtil.getClient(token);
    }

    /*
     *  返回新token令牌
     */
    @PostMapping("/refresh/by")
    public Result refreshBy(@RequestHeader(SecurityConstants.AUTH_TOKEN) String token){
        Client client = refreshService.selectClientById(JWTUtil.getClient(token).getId());
        return new Result(true,StatusCode.OK,"注册成功", JWTUtil.sign(JWTUtil.getMap(client)));
    }

    @PostMapping("/refresh/token")
    public String refreshToken(@RequestParam("id")Integer id){
        System.out.println(id);
        Client client = refreshService.selectClientById(id);
        return JWTUtil.sign(JWTUtil.getMap(client));
    }
}
